package com.czx.service.impl;

import com.czx.common.constants.CookieKey;
import com.czx.common.constants.RedisKeys;

import com.czx.entity.Message;
import com.czx.service.LikeService;
import com.czx.service.PostScoreService;
import com.czx.service.SysMessageService;
import com.czx.vo.LikeVo;
import com.czx.vo.UserToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.SetOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.stereotype.Service;

@Service
public class LikeServiceImpl implements LikeService {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private PostScoreService postScoreService;
    @Autowired
    private SysMessageService sysMessageService;

    @Override
    public Double getScore(UserToken userToken, Integer id) {
        ZSetOperations<String, String> zset = stringRedisTemplate.opsForZSet();
        //目标用户的key
        String fansKey = String.format(RedisKeys.USER_FANS_KEY, id);
        //当前用户的key
        String followKey = String.format(RedisKeys.USER_FOLLOWER_KEY, userToken.getUser().getId());
        //查看目标用户的key下有没有当前用户的key
        Double score = zset.score(fansKey, userToken.getUser().getId().toString());
        return score;
    }


    /**
     * 实现点赞
     *
     * @param likeVo
     */
    @Override
    public void click(LikeVo likeVo) {
        //1.使用set保存点赞的数据
        SetOperations<String, String> setOperations = stringRedisTemplate.opsForSet();
        //准备点赞的key
        String key = null;
        // entityType点赞类型，1：帖子，2：评论
        if (likeVo.getEntityType() == 1) {
            key = String.format(RedisKeys.USER_LIKE_KEY, CookieKey.POST, likeVo.getEntityId());
        } else {
            key = String.format(RedisKeys.USER_LIKE_KEY, CookieKey.COMMENT, likeVo.getEntityId());
        }
        //准备用户点赞数量的key
        String userLikeKey = String.format(RedisKeys.USER_LIKE_KEY, CookieKey.USER, likeVo.getUserId());
        //判断一下当前用户是否对实体点过赞了
        Boolean member = setOperations.isMember(key, likeVo.getUserId().toString());
        if (member) {
            // 如果已经存在说明点过赞了，就要取消点赞
            setOperations.remove(key, likeVo.getUserId().toString());
            stringRedisTemplate.opsForValue().decrement(userLikeKey);
        } else {
            // 实现点赞
            setOperations.add(key, likeVo.getUserId().toString());
            stringRedisTemplate.opsForValue().increment(userLikeKey);
            addLike(likeVo);
        }

        //点赞操作存入redis
        if (likeVo.getEntityType() == 1) {
            postScoreService.addPostIdToRedis(likeVo.getEntityId());
        }
    }

    /**
     * 给message表增加点赞数据
     */
    public void addLike(LikeVo likeVo){
        Message message=new Message();
        message.setFromId(likeVo.getUserId());
//        sysMessageService.addLike()
    }

    //返回点赞实体的数量
    public Long getLikeCount(Integer type, Integer id) {
        String key = null;
        if (type == 1) {
            key = String.format(RedisKeys.USER_LIKE_KEY, CookieKey.POST, id);
        } else if (type == 2) {
            key = String.format(RedisKeys.USER_LIKE_KEY, CookieKey.COMMENT, id);
        }
        return stringRedisTemplate.opsForSet().size(key); // 传递一个key返回长度
    }

}
